#include <QFileDialog>
#include <QSettings>
//#include <QMessageBox>
#include "NewProject.h"
#include "ui_NewProject.h"
#include "Include/Settings.h"

NewProject::NewProject(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewProject)
{
    ui->setupUi(this);
    connect(ui->btnSelectPath, SIGNAL(clicked()), SLOT(onProjectPath()));
    connect(ui->buttonBox, SIGNAL(accepted()), SLOT(onAccepted()));
    connect(ui->edtProjectPath, SIGNAL(textChanged(QString)), SLOT(onProjectPathChanged(QString)));

    ui->cmbVersion->addItem("2", 2);
    ui->cmbVersion->addItem("3", 3);
    ui->cmbVersion->setCurrentIndex(ui->cmbVersion->findData(3));
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
}

NewProject::~NewProject()
{
    delete ui;
}

void NewProject::onProjectPath()
{
    QFileDialog d(this, tr("Create project"), Plugins::Settings::instance()->lastUsedWorkspace(), tr("Project files (*.pynotoprj)"));
    d.setDefaultSuffix("pynotoprj");
    d.setAcceptMode(QFileDialog::AcceptSave);
    if (d.exec() == QDialog::Accepted){
        Plugins::Settings::instance()->setLastUsedWorkspace(QFileInfo(d.selectedFiles()[0]).absoluteDir().path());
        ui->edtProjectPath->setText(d.selectedFiles()[0]);
    }
}

void NewProject::onProjectPathChanged(const QString & text)
{
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!text.isEmpty());
}

QString NewProject::path()
{
    return ui->edtProjectPath->text();
}

void NewProject::onAccepted()
{
    //if (_path.isEmpty()){
    //    QMessageBox::critical(this, tr("Empty path"), tr("Project path cannot be empty."));
    //}
    QSettings set(ui->edtProjectPath->text(), QSettings::IniFormat);
    set.setValue("common/name", ui->edtProjectName->text());
    set.setValue("common/pyVersion", ui->cmbVersion->itemData(ui->cmbVersion->currentIndex()).toInt());
    accept();
}
